import { _decorator, Color, Component, Label, Node, sp, Sprite, v2, v3 } from 'cc';
import { convertNumber, getDistance, randomNum, trackTarget } from './utils/utils';
import { GameMgr } from './GameMgr';
const { ccclass, property } = _decorator;

@ccclass('Fish')
export class Fish extends Component {

    speedMove=130;
    targetPos=v3();

    exp=0;
    currSelfFish=0;

    private isFrozenIng = false;//是否被冻结
    get IsFrozenIng() {
        return this.isFrozenIng;
    }
    set IsFrozenIng(value) {
        this.isFrozenIng = value;
        if (value==true) {
            this.node.getChildByName("sp").getComponent(sp.Skeleton).color=Color.BLUE;
        }
        else{
            this.node.getChildByName("sp").getComponent(sp.Skeleton).color=Color.WHITE;
        }
    }


    pursueTime=0;
    private isPursueIng=false;//是否正在追击玩家
     isRunAway=false;//是否正在逃跑

    get IsPursueIng() {
        return this.isPursueIng;
    }
    set IsPursueIng(value) {
        this.isPursueIng = value;
        if (value == true) {
            console.log("追击")
            this.node.getChildByName("sp").getComponent(sp.Skeleton).setAnimation(0,"eat",true);
            this.targetPos = null;

            this.schedule(()=>{
                this.speedMove+=25
            },0.7,3,1)
        }
        else {
            this.node.getChildByName("sp").getComponent(sp.Skeleton).setAnimation(0,"idle",true);
            this.speedMove-=75
            this.pursueTime = 0;
            this.targetPos = this.getNextTargetPos();
        }
    }


    start() {
        this.targetPos = this.getNextTargetPos();
        let skeComp=this.node.getChildByName("sp").getComponent(sp.Skeleton)
        skeComp.setCompleteListener((trackEntry) => {
            let name = trackEntry.animation ? trackEntry.animation.name : '';
            if (name === "eat") {
                skeComp.setAnimation(0, "idle", true)
            }
        });
    }

    protected update(dt: number): void {
        if (GameMgr.ins.isGameIng&&this.IsFrozenIng==false) {
            if (this.targetPos) {
                this.biontTrackTargetMove(this.targetPos, this.node, this.speedMove, dt);
            }
    
            if (this.IsPursueIng==true) {
                this.pursueTime+=dt;
    
                if (this.pursueTime>5) {
                    this.IsPursueIng=false;
                }
                if (this.exp<GameMgr.ins.Exp) {
                    this.IsPursueIng=false;
                }
                // if (this.node.position.x) {
                    
                // }
                this.biontTrackTargetMove_Pursue(GameMgr.ins.player.position, this.node, this.speedMove, dt);
            }
        }
       
    }

    tempPos = v3();

    init(_currSelfFish, expNum) {//初始化给鱼的经验，初始化他的大小，鱼类spine不在初始化里
        this.currSelfFish=_currSelfFish;
        this.exp = expNum;
        this.node.getChildByName("volumeNumLabel").getComponent(Label).string = `${GameMgr.ins.getFishName(this.currSelfFish[0])}:${convertNumber(this.exp)}`
        this.changeSelfScare();
    }

    changeSelfScare(){
        let preScale = this.node.scale;
        if (this.currSelfFish[1] == 1) {
            this.node.scale = v3(preScale.x + 0.15, preScale.y + 0.15, 1);
        }
        else if (this.currSelfFish[1] == 2) {
            this.node.scale = v3(preScale.x + 0.3, preScale.y + 0.3, 1);
        }
    }

    biontTrackTargetMove(targetPos, selfNode, speed, dt) {

        if (!targetPos) {
            return
        }

        let speedArr = trackTarget(targetPos.x, targetPos.y, selfNode.position.x, selfNode.position.y, speed * dt);

        selfNode.getPosition(this.tempPos);
        this.tempPos.x += speedArr[0]
        this.tempPos.y += speedArr[1]
        selfNode.setPosition(this.tempPos);

        selfNode.angle = speedArr[2] * (180 / Math.PI);


        //如果自己已经到达附近
        if (getDistance(selfNode.position.x, selfNode.position.y, targetPos.x, targetPos.y) < 50) {
            let nextTartgetPos = this.getNextTargetPos();
            this.targetPos = nextTartgetPos;
        }
    }

    biontTrackTargetMove_Pursue(targetPos, selfNode, speed, dt) {
        if (!targetPos) {
            return
        }

        let speedArr = trackTarget(targetPos.x, targetPos.y, selfNode.position.x, selfNode.position.y, speed * dt);

        selfNode.getPosition(this.tempPos);
        this.tempPos.x += speedArr[0]
        this.tempPos.y += speedArr[1]
        selfNode.setPosition(this.tempPos);

        selfNode.angle = speedArr[2] * (180 / Math.PI);


        //如果自己已经到达附近
        if (getDistance(selfNode.position.x, selfNode.position.y, targetPos.x, targetPos.y) < 50) {
            this.IsPursueIng = false;
            this.node.getChildByName("sp").getComponent(sp.Skeleton).setAnimation(0, "eat", false);
            GameMgr.ins.gameOver();

        }
    }

    getNextTargetPos() {
        let res = null;
        while (!res) {
            // if (this.biontType <= 6) {
            //     let tempRes = v2(this.node.position.x + gameMgr.ins.randowNum(-2000, 2000), this.node.position.y + gameMgr.ins.randowNum(-2000, 2000))
            //     if (Math.abs(tempRes.x)>gameMgr.ins.mapBoundary.x||Math.abs(tempRes.x)>gameMgr.ins.mapBoundary.y) {
            //         continue;
            //     }
            //     else{
            //         res=tempRes;
            //     }
            // }
            // else {
            let tempRes = v2(this.node.position.x + randomNum(-1000, 1000), this.node.position.y + randomNum(-1000, 1000))
            if (Math.abs(tempRes.x) > GameMgr.ins.mapBoundary.x || Math.abs(tempRes.y) > GameMgr.ins.mapBoundary.y) {
                continue;
            }
            else {
                res = tempRes;
            }
            // }

            return res;
        }

    }

    //遇到玩家逃跑
    meetPlayerRunAway() {
        if (this.isRunAway==false) {
            console.log("加速")
            this.speedMove += 80;
            this.isRunAway=true;
            this.targetPos=v3(this.node.position.x*2-GameMgr.ins.player.position.x,this.node.position.y*2-GameMgr.ins.player.position.y,0);
            // this.node.getChildByName("sp").getComponent(sp.Skeleton).color = Color.RED
    
            this.scheduleOnce(() => {
                this.speedMove -= 80;
                // this.node.getChildByName("sp").getComponent(sp.Skeleton).color = Color.WHITE
                this.isRunAway=false;
    
            }, 4)
        }
       
    }

    //遇到玩家追击
    meetPlayerPursue() {
        if (this.IsPursueIng==false) {
         
            this.IsPursueIng = true;
        }

    }


}


